<template>
  <div>
    <!-- 经销商收车地址维护 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <FilterTables
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      columnsRef="zjVehicleTransfer"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" @click="addHandle(1)">新 增</multiuse-button>
        <multiuse-button size="small" type="primary" :click="addHandle.bind(this, 2)" :disabled="isSelected">批量确认</multiuse-button>
        <multiuse-button size="small" type="primary" @click="addHandle(3)">移库导入</multiuse-button>
      </template>
    </FilterTables>
    <Drawer :visible.sync="isEmit" :title="'编辑'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <emitModel :rowData="formData" @close="emitcloseHandler" />
    </Drawer>
    <Drawer :visible.sync="isAdd" :title="'新增'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <addModel @close="addcloseHandler" />
    </Drawer>
    <BaseDialog :visible.sync="visible" :title="'导入'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <importFiles :rowData="formData" @close="importcloseHandler" />
    </BaseDialog>
  </div>
</template>
<script>
import { getList, postConfirm, getWarehouseList } from '@/api/omSale/HeadquartersInventory/VehicleTransfer/index.js';
import emitModel from './emitModel';
import addModel from './addModel';
import importFiles from './importFiles';
import { dictionary } from '@/utils/dictMixin';
export default {
  name: 'TemplateDistribution',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: getList,
      fetchParams: {
        xhrAbort: true
      },
      formData: {},
      isEmit: false,
      isAdd: false,
      visible: false,
      isSelected: true,
      selectedList: []
    };
  },
  components: {
    emitModel,
    addModel,
    importFiles
  },
  mixins: [dictionary],
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getList();
  },
  methods: {
    createdFrom() {
      return [
        {
          type: 'TEXT_AREA',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入',
          maxlength: '',
          maxRows: 2
        },
        {
          type: 'INPUT',
          label: '移库人',
          fieldName: 'transferPersonName',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '移库原因',
          fieldName: 'transferReason',
          placeholder: '请输入'
        },
        {
          type: 'RANGE_DATE',
          label: '移库日期',
          fieldName: 'beginTransferDate|endTransferDate',
          placeholder: '请选择',
          initialValue: [],
          dateType: 'exactdaterange'
        },
        {
          type: 'INPUT',
          label: '车系名称',
          fieldName: 'seriesName',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '工厂库',
          fieldName: 'status',
          placeholder: '请选择',
          itemList: this.createDictList('1003')
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row, 1)} disabled={props.row.transferStatus == 60391001}>
                  编辑
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '状态',
          dataIndex: 'transferStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6039'),
          editItems: this.createDictList('6039')
        },
        {
          title: '移库单号',
          dataIndex: 'transferNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          align: 'center',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车系名称',
          dataIndex: 'seriesName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '移库人',
          dataIndex: 'transferPersonName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '原仓库',
          dataIndex: 'fromWarehouseName',
          sorter: true,
          filter: true,
          filterType: 'checkbox'
        },
        {
          title: '新仓库',
          dataIndex: 'toWarehouseName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '移库日期',
          dataIndex: 'transferDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },

        {
          title: '车辆状态',
          dataIndex: 'vehicleStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6040'),
          editItems: this.createDictList('6040')
        },
        {
          title: '移库原因',
          dataIndex: 'transferReason',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    selectChangeHandle(row) {
      if (row.length > 0) {
        this.isSelected = false;
        let is = true;
        row.map(x => {
          if (x.transferStatus == 60391001) {
            // 如果有移库的就变为false
            is = false;
          }
        });
        this.selectedList = row;
        // 如果有移库的就disabled
        this.isSelected = is ? false : true;
      } else {
        this.isSelected = true;
        this.selectedList = [];
      }
    },
    async getList() {
      let res = await getWarehouseList();
      this.columns.find(item => item.dataIndex === 'fromWarehouse').editItems = res.data.list.map(x => ({ text: x.warehouseName, value: x.warehouseId }));
      this.columns.find(item => item.dataIndex === 'toWarehouse').editItems = res.data.list.map(x => ({ text: x.warehouseName, value: x.warehouseId }));
      this.columns.find(item => item.dataIndex === 'fromWarehouse').filterItems = res.data.list.map(x => ({ text: x.warehouseName, value: x.warehouseId }));
      this.columns.find(item => item.dataIndex === 'toWarehouse').filterItems = res.data.list.map(x => ({ text: x.warehouseName, value: x.warehouseId }));
    },
    async addHandle(type) {
      if (type == 1) {
        this.isAdd = true;
      } else if (type == 2) {
        await this.$confirm('是否确认移库?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            let arr = [];
            this.selectedList.map(x => {
              arr.push(x.id);
            });
            let res = await postConfirm(arr);
            if (res.resultCode == 200) {
              this.$notify({
                title: '成功',
                message: '移库成功',
                type: 'success'
              });
              this.fetchParams = Object.assign({}, this.fetchParams);
            }
          })
          .catch(() => {});
      } else {
        this.visible = true;
      }
    },
    editHandle(data) {
      this.formData = data;
      this.isEmit = true;
    },
    addcloseHandler() {
      this.isAdd = false;
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    emitcloseHandler() {
      this.isEmit = false;
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    importcloseHandler() {
      this.visible = false;
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    changeHandle(params) {
      this.fetchParams.xhrAbort = false;
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    }
  }
};
</script>
